package com.qimo.zzy.controller;

import com.qimo.zzy.entity.ResultEntity;
import com.qimo.zzy.service.UserService;
import com.qimo.zzy.tool.KoDoTool;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;

@CrossOrigin//跨域
@RestController//遵循RestFul
@RequestMapping("qimo/Tool/")
public class ToolController {
    private static final Logger logger = LoggerFactory.getLogger(ToolController.class);//日志getLogger需要传入的类名

    @Resource
    KoDoTool koDoTool;

    @Resource
    UserService userService;
    @Value("${project.localfilepath}")
    private String localFilePath;//本地文件存储地址

    @PostMapping("upFile")
    private String upFile(
            @RequestParam("uid") int uid,
            @RequestParam("file") MultipartFile file
    ) {
        String SuffixName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));//获取文件后缀名
        String FileNewName = UUID.randomUUID() + SuffixName;//新的文件名
        File dest = new File(localFilePath + FileNewName);//生成新的文件
        if (!dest.getParentFile().exists()) {//判定文件夹是否存在
            dest.getParentFile().mkdir();//生成文件夹
        }
        try {
            file.transferTo(dest);//将临时文件转为正式文件
            String koDoString = koDoTool.UploadFile(localFilePath + FileNewName);//上传到七牛云
            userService.changeAvatat(uid, koDoString);
            return ResultEntity.success(koDoString);//返回七牛云的文件路径字符串
        } catch (Exception e) {
            logger.error(e.getMessage());//输出日志
            return ResultEntity.fail(-10001);//返回错误代码
        }
    }
    @GetMapping("img")
    private String img(){
        return "{\"data\":[\"../../static/lunbo/A.jpg\",\"../../static/lunbo/B.jpg\",\"../../static/lunbo/C.jpg\"]}";
    }
//    @GetMapping("Classify")
//    private String classify(){
//        return "{\"data\":[]}"
//                '{"message":[{"name":"分类","image_src":"https://api-hmugo-web.itheima.net/pyg/icon_index_nav_4@2x.png","open_type":"switchTab","navigator_url":"/pages/category/main"},{"name":"秒杀拍","image_src":"https://api-hmugo-web.itheima.net/pyg/icon_index_nav_3@2x.png"},{"name":"超市购","image_src":"https://api-hmugo-web.itheima.net/pyg/icon_index_nav_2@2x.png"},{"name":"母婴品","image_src":"https://api-hmugo-web.itheima.net/pyg/icon_index_nav_1@2x.png"}],"meta":{"msg":"获取成功","status":200}}')
//    }
}
